<?php
namespace app\admini\controller;
use app\admini\controller\Base;
use think\Controller;
use think\Db;
use think\request;
use think\Session;

class Teacher extends Base
{
	
    public function index($keyword='',$tel='',$grade='')
    {
    	$map = array();
		if ($keyword) {
			$map['a.teacher_name'] = array(
				'like',
				'%' . $keyword . '%'
			);
		}
		$this->assign('keyword', $keyword);
		if ($tel) {
			$map['a.tel'] = $tel;
		}
		$this->assign('tel', $tel);
		if ($grade) {
			$map['a.grade'] = $grade;
		}
		$this->assign('grade', $grade);
		
    	$res = db('oa_teacher')->alias('a')->field('a.*,w.class_name')->join('oa_classname w','a.grade = w.id')->where($map)->order('a.add_time desc')->paginate(5, false, get_query());
    	$this->assign('data',$res);
    	$type = Db::name('oa_classname')->select();
		$this->assign('type',array2level($type));
		return $this->fetch();
    }
	
	public function add_tea()
	{
		if($this->request->isPost()){
			$data = $this->request->post();
			unset($data['file']);
			$data['add_time']=now_time();//admini
			$res = db('oa_teacher')->insert($data);
			if($res){
				return $this->toSuccess('/admini/teacher/index');
			}else{
				return $this->toError();
			}
		}else{
			$res = db('oa_classname')->select();
			$this->assign('type',array2level($res));
			return $this->fetch();	
		}
		
	}
	
	public function updata_tea()
	{
		if($this->request->isPost()){
			$data = $this->request->post();
			unset($data['file']);
			$res = db('oa_teacher')->updata($data);
			if($res){
				return $this->toSuccess('/admini/teacher/index');
			}else{
				return $this->toError();
			}
		}else{
			$res = db('oa_teacher')->find(input('id'));
			$this->assign('data',$res);
			$res = db('oa_classname')->select();
			$this->assign('type',array2level($res));
			return $this->fetch();	
		}
		
	}
	

}
